﻿open System
open System.ServiceProcess
open System.Data.SqlClient
open System.Data

module Program =

    type Position = 
        {
            StopLossLimit : decimal
            ProfitTakingLimit : decimal
        }

    let (?) (this : SqlDataReader) name =  this.GetOrdinal(name) |> this.GetValue |> unbox

    let startStrategiesTracking() =
        use connection = new SqlConnection("Data Source=.\SQLExpress;Initial Catalog=Trading;Integrated Security=True")
        connection.Open()

        let positions = 
            let cmd = new SqlCommand("SELECT * FROM Position", connection)
            let reader = cmd.ExecuteReader()
            [| 
                while reader.Read() do 
                    yield { StopLossLimit = reader?StopLossLimit; ProfitTakingLimit = reader?ProfitTakingLimit }
            |]

        ()


    let windowsService = 
        {
            new ServiceBase() with
                override this.OnStart _ = startStrategiesTracking()
        }

    [<EntryPoint>]
    let main _ = 
        if Environment.UserInteractive then
            startStrategiesTracking()
        else
            ServiceBase.Run windowsService
        0


